

/*global AJAXBroker */


var ArtistList = Class.create();
ArtistList.prototype = {
		initialize : function() {
			this.artists = [];
			this.totalresults = 0;
			this.size = 0;
			this.error;
		},

		addArtist : function(artist) {
			this.artists.push(artist);
		},
		setError : function(errorParam) {
			this.error = errorParam;
		},
		setTotalResults : function(totalresultsParam) {
			this.totalresults = totalresultsParam;
		},
		getArtists : function() {
			return this.artists;
		},
		getTotalResults : function() {
			return this.totalresults;
		},
		getSize : function() {
			return this.size;
		},
		incrementSize : function() {
			this.size += 1;
		}
};
var ArtistDTO = Class.create();
ArtistDTO.prototype = {
		initialize : function(name, url) {
			this.name = name;
			this.url = url;
			this.image = null;
			this.mbid = null;
		},
		setName : function(name) {
			this.name = name;
		},
		setUrl : function(url) {
			this.url = url;
		},
		setImage : function(imageParam) {
			this.image = imageParam;
		},
		setMbid : function(mbid) {
			this.mbid = mbid;
		},
		getUrl : function() {
			return this.url;
		},
		getName : function() {
			return this.name;
		},
		getImage : function() {
			return this.image;
		},
		getMbid : function() {
			return this.mbid;
		}
};
var ArtistInfoDTO = Class.create();
ArtistInfoDTO.prototype = Object.extend(new ArtistDTO(), {
		initialize : function() {
			this.summary;
		},
		setSummary : function(summary) {
			this.summary = summary;
		},
		getSummary : function() {
			return this.summary;
		}
});

var ArtistRepository = Class.create();
ArtistRepository.prototype = {

		initialize : function() {
			this.ajaxBroker=null;
		},

		list : function(artistName, limit, callback) {

			var url = LastFM.url(LastFM.ARTIST_SEARCH, {
					"artist" :artistName,
					"limit" :limit
			});

			this.ajaxBroker=new AjaxBroker(url, "get", function(xmlObject) {
				var artists = new XMLParser().parseArtistSearch(xmlObject);
				return callback(artists);
			}.bind(this), null).call();

			return null;
		},
		info : function(artistName, mbid, callback) {

			var url = LastFM.url(LastFM.ARTIST_INFO, {
					"artist" :artistName,
					"mbid" :mbid
			});

			this.ajaxBroker=new AjaxBroker(url, "get", function(xmlObject) {
				var artistInfo = new XMLParser().parseArtistInfo(xmlObject);
				return callback(artistInfo);
			}.bind(this), null).call();

			return null;
		}

};

var ArtistListService = Class.create();
ArtistListService.prototype = {

		initialize : function(artistRepository) {
			this.artistRepository = artistRepository;
		},

		list : function(artistName, limit, callback) {
			try {
				this.artistRepository.list(artistName, limit, callback);
			} catch (exp) {
				Logger.debug(exp);
			}
		}

};

var ArtistInfoService = Class.create();
ArtistInfoService.prototype = {

		initialize : function(artistRepository) {
			this.artistRepository = artistRepository;
		},

		info : function(artistName, mbid, callback) {
			try {
				this.artistRepository.info(artistName, mbid, callback);
			} catch (exp) {
				Logger.debug(exp);
			}
		}

};

var ArtistListView = Class.create();
ArtistListView.prototype = {

		initialize : function(artistRepository) {
			this.artistRepository = artistRepository;
			this.artistListService = new ArtistListService(artistRepository);
			this.artistInfoService = new ArtistInfoService(artistRepository);
			
		},
		load : function() {
			// add input text and button to the panel
		var searchDiv = new Element('div', {
			id :'ArtistSearch'
		});

		var searchInput = new Element('input', {
				type :'text',
				'class' :'searchInput'
		});
		searchDiv.insert(searchInput);

		var searchButton = new Element('input', {
				type :'button',
				value :'search',
				'class' :'searchButton'
		});
		searchButton.observe('click', function() {
			try{
			this.list(searchInput.value, "10");
			}catch(Exp){
				Logger.error("list "+exp);
			}
		}.bind(this));
		searchDiv.insert(searchButton);

		$('Panel').insert(searchDiv);

		

	},
	list : function(artistName, limit) {

		this.artistListService.list(artistName, limit, function(artists) {
			if($('ArtistList') !== null){
				$('ArtistList').remove();
			}
			var listDiv = new Element('div', {
				id :'ArtistList'
			});
			listDiv.hide();
			$('Panel').insert(listDiv);
			
			if (artists.getTotalResults() > 0) {
				$('ArtistList').insert(new Element('p', {
					'class' :'header'
				}).update(artists.getTotalResults() + ' entries are found'));
			}
			
			artists.getArtists().each( function(artist) {

				var h2=new Element('h2',{'class':'accordion_toggle'});
				var url = new Element('a', {
					'class' :'artistUrl',
					href :'javascript:void(0)'
				});
				url.update(artist.getName());
				h2.insert(url);
			
				var content= new Element('div', {
					'class' :'accordion_content'
				});
				
				var summary = new Element('div');
				content.insert(summary);
				
				h2.observe('click', function() {
					if (summary.empty()) {
						this.artistInfoService.info(artist.getName(), artist.getMbid(), function(artistInfo) {
							summary.update(artistInfo.getSummary());
						}.bind(this));
					}
				}.bind(this));
				$('ArtistList').insert(h2);
				$('ArtistList').insert(content);

			}.bind(this));

			$('ArtistList').insert(new Element('p', {
				'class' :'footer'
			}).update(artists.getSize() + ' entries are listed'));
			$('ArtistList').show();
			
			var bottomAccordion = new accordion('ArtistList');
			
			var verticalAccordions = $$('.accordion_toggle');
			verticalAccordions.each(function(accordion) {
				$(accordion.next(0)).setStyle({
				  display: 'none'
				});
			});
		
		}.bind(this));
	}

};
